﻿namespace WenAntdUI.Services;

public class AdminService(IFreeSql fsql, IdentityService identity, MenuService menu)
{
    /// <summary>
    /// 查看页面下全部权限
    /// </summary>
    /// <param name="pageCode"></param>
    /// <returns></returns>
    public List<string> AuthorizingPageBlock(string pageCode)
    {
        var username = identity.UserName;

        var aus = fsql.Select<SysUserRole, SysRoleMenu, SysMenu>()
            .LeftJoin(w => w.t1.RoleName == w.t2.RoleName)
            .LeftJoin(w => w.t2.MenuCode == w.t3.Code)
            .Where(w => w.t1.UserName == username)
            .Where(w => w.t3.ParentCode == pageCode)
            .OrderBy(w => w.t3.OrderNo)
            .ToList(w => w.t3.Code);

        return aus;
    }

    /// <summary>
    /// 查看用户是否具备指定权限
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    public bool AuthorizingBlock(string code)
    {
        var username = identity.UserName;
        var menus = fsql.Select<SysUserRole, SysRoleMenu, SysMenu>()
            .LeftJoin(w => w.t1.RoleName == w.t2.RoleName)
            .LeftJoin(w => w.t2.MenuCode == w.t3.Code)
            .Where(w => w.t1.UserName == username)
            .Where(w => w.t2.MenuCode == code)
            .OrderBy(w => w.t3.OrderNo)
            .ToList(w => w.t3);

        return menus.Count > 0;
    }
}
